package com.example.optimizaapp;

import java.util.ArrayList;
import java.util.Arrays;

import com.example.optimizaapp.domain.Incidente;
import com.example.optimizaapp.domain.ManejadorCliente;
import com.example.optimizaapp.domain.ManejadorIncidente;
import com.example.optimizaapp.domain.ManejadorUsuario;
import com.example.optimizaapp.domain.Utilities.Constants;
import com.example.optimizaapp.domain.Utilities.IncidentesByUserService;
import com.slidingmenu.lib.*;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ProgressDialog;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity{
	
	private ListView mainListView;
	ManejadorIncidente mi;
	ManejadorCliente mc;
	ManejadorUsuario mu;
	private ArrayAdapter<String> listAdapter ;
	SlidingMenu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mi= ManejadorIncidente.getManejadorIncidente();
        mi.setIncidentes(new ArrayList<Incidente>());
        mu= ManejadorUsuario.getManejadorUsuario();
        
        mc = ManejadorCliente.getManejadorCliente();
        if (mc.getClientes()==null){
        	mc.obtenerClientes();
        }
                               
        //configure the SlidingMenu        
        this.setSlidingMenu();
        
        IncidentesByUserService serviceIncidente =new IncidentesByUserService(ProgressDialog.show(MainActivity.this, "En Progreso", "Cargando"), this);
        String url = Constants.SERVICIO_INCIDENTES + "id=" + mu.getUsuarioLogueado().getId();
        serviceIncidente.execute(url);        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuItem item = menu.add("Menu");
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
               
        return true;
    }
    
   @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	menu.toggle();
    	    	
    	
    	return false;
    };
    
  
		
		public void setSlidingMenu(){
			 // configure the SlidingMenu
	        menu = new SlidingMenu(this);
	        menu.setMode(SlidingMenu.RIGHT);
	        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
	        menu.setShadowWidthRes(R.dimen.shadow_width);
	        //menu.setShadowDrawable(R.drawable.shadow);
	        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
	        menu.setFadeDegree(0.35f);
	        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
	        menu.setMenu(R.layout.activity_main_menu);
		}
		
		public void populateTable(){
			 this.mainListView=(ListView) findViewById(R.id.listViewClientes);
			    // Create and populate a List of clients.
			 			 
			 final String[] incidentes = new String[mi.getIncidentes().size()];
			 
			 for (int i = 0; i<mi.getIncidentes().size(); i++){
				 Incidente in = mi.getIncidentes().get(i);
				 String incidenteString = in.getIdIncidente() + " - " + in.getTituloIncidente();
				 incidentes[i]= incidenteString;
			 }		
			 
			 ArrayList<String> listaClientes = new ArrayList<String>();
			 listaClientes.addAll(Arrays.asList(incidentes));
			 // Create ArrayAdapter using the clients list.
			 listAdapter= new ArrayAdapter<String>(this, R.layout.simple_row, listaClientes);
			 this.mainListView.setAdapter(listAdapter);
			 this.mainListView.setOnItemClickListener(new OnItemClickListener() {
		            public void onItemClick(AdapterView<?> parent, View view,
		                    int position, long id) {

		            	Intent intent = new Intent(getBaseContext(), DetalleIncidente.class);
		            	intent.putExtra("INDEX", position);
		            	startActivity(intent);
		            }
		        });
		}
		
		public void pressedBuscarIncidente(View view){
			Intent intent = new Intent(this, BuscarIncidenteActivity.class);
	    	startActivity(intent);
		}
		
		public void pressedBuscarEmpresa(View view){
			Intent intent = new Intent(this, BuscarEmpresa.class);
	    	startActivity(intent);
		}

}
